package yxy.game.pm2.command.threelinemutualpush;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.pusher;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ThreeLineMutualPushMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.threelinepush.ThreeLinePushModule;

import java.util.ArrayList;
import java.util.Collection;

/****************************************
 * @name CMD_ThreeLineMutualPushBonusReceive
 * @description 三线互推小游戏页面信息
 * @author 梁健彬
 * @since 2022-08-17
 ***************************************/
@MessageType(proto = Command.ThreeLineMutualPushBonusReceive, classtype = ThreeLineMutualPushMessage.ThreeLineMutualPushBonusReceiveRequest.class)
public class CMD_ThreeLineMutualPushBonusReceive extends Command<ThreeLineMutualPushMessage.ThreeLineMutualPushBonusReceiveRequest> {

    @Override
    public void execute(MessageSource messageSource, ThreeLineMutualPushMessage.ThreeLineMutualPushBonusReceiveRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int id = request.getId();//奖励id
        ThreeLineMutualPushMessage.ThreeLineMutualPushBonusReceiveResponse.Builder response = ThreeLineMutualPushMessage.ThreeLineMutualPushBonusReceiveResponse.newBuilder();

        try {
            ThreeLinePushModule module = ThreeLinePushModule.get(playerCode);

            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            if (module.getCurrentMapId() <= id) {
                send(messageSource, response, Result.DataError);//开挂
                return;
            }

            ArrayList<Integer> rewardIds = module.getRewardIds();
            if(rewardIds.contains(id)){
                send(messageSource, response, Result.DataError);//重复领取
                return;
            }

            //todo 发奖励并记录
            pusher data = DataCenter.getData(id, pusher.class);
            if(data ==null ){
                send(messageSource, response, Result.DataError);//不存在
                return;
            }

            Collection<Currency> rewards = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());

            new CurrencyHandle().addCurrency(playerCode, CCReason.三线互推, rewards);

            rewardIds.add(id);

            module.set();
            response.addAllRewards(new CurrencyHandle().packItems(rewards));
            response.setCurrentMapId(module.getCurrentMapId());
            response.addAllHasReceiveIds(module.getRewardIds());

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);

        }
    }

    private void send(MessageSource messageSource, ThreeLineMutualPushMessage.ThreeLineMutualPushBonusReceiveResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
